﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SiteLib.Data;
using SiteLib.Tools;

namespace SiteLib
{
    public partial class InfoList : System.Web.UI.Page
    {
        public int CurrentYear { get; set; }

        public int CurrentType { get; set; }

        protected void Page_Init(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["type"]))
            {
                string sType = Request.QueryString["type"];
                int iType;

                if (int.TryParse(sType, out iType))
                {
                    CurrentType = iType;
                    CurrentYear = FillYears(iType);
                    FillMessages();
                }
            }
            else
            {
                Response.Redirect("/");
            }

            if (!String.IsNullOrEmpty(Request.QueryString["year"]))
            {
                string sYear = Request.QueryString["year"];
                int iYear;

                if (int.TryParse(sYear, out iYear))
                {
                    CurrentYear = iYear;
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!String.IsNullOrEmpty(Request.QueryString["type"]))
                {
                    string sType = Request.QueryString["type"];
                    int iType;

                    if (int.TryParse(sType, out iType))
                    {
                        CurrentType = iType;
                        CurrentYear = FillYears(iType);
                        FillMessages();
                    }
                }
                else
                {
                    Response.Redirect("/");
                }
                
                if (!String.IsNullOrEmpty(Request.QueryString["year"]))
                {
                    string sYear = Request.QueryString["year"];
                    int iYear;

                    if (int.TryParse(sYear, out iYear))
                    {
                        CurrentYear = iYear;
                        FillMessages();
                    }
                }
                else if (!String.IsNullOrEmpty(Request.QueryString["Id"]))
                {
                    pnlYearMenu.Visible = false;

                    string sSiteContentId = Request.QueryString["Id"];
                    int iSiteContentId;

                    if (int.TryParse(sSiteContentId, out iSiteContentId))
                    {
                        SiteContent sc = SiteContent.Get(iSiteContentId);
                        if (sc != null)
                        {
                            mvCompanyMessages.ActiveViewIndex = 1;
                            lMainContent.Text = sc.Content;
                            lMainContentTitle.Text = sc.Title;
                            lCreatedDate.Text = sc.CreateDate.HasValue ? sc.CreateDate.Value.ToLongDateString() : String.Empty;

                            rptFiles.DataSource = sc.FileAttachements;
                            rptFiles.DataBind();
                        }
                    }
                }
            }
        }

        private int FillYears(int typeCode)
        {
            IOrderedEnumerable<int> lYearList = SiteContent.ListAllContentYear(typeCode).OrderByDescending(i => i);
            rptSubMenu.DataSource = lYearList;
            rptSubMenu.DataBind();

            return lYearList.First();
        }

        private void FillMessages()
        {
            mvCompanyMessages.ActiveViewIndex = 0;

            SiteContentType sc = SiteContentType.Get(CurrentType);
            if (sc != null)
            {
                lMainContentTitle.Text = String.Format("{0} ({1})", sc.Title, CurrentYear);
            }

            List<SiteContent> scList = SiteContent.ListAllContent(CurrentType, CurrentYear);
            rptMessages.DataSource = scList.OrderByDescending(t => t.CreateDate);
            rptMessages.DataBind();
        }

        protected string GetYearStatus(object oYear)
        {
            if (oYear is int)
            {
                int iYear = Convert.ToInt32(oYear);
                return iYear == CurrentYear ? "active" : string.Empty;
            }
            return string.Empty;
        }
    }
}